package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfContractInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfContractInfo;
import com.dhcc.bpm.modules.scf.mapper.ScfSubjectBasicInfoMapper;
import com.dhcc.bpm.modules.scf.service.IScfContractInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfContractInfoVO.ScfGetContractDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfContractInfoVO.ScfGetContractListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@AllArgsConstructor
public class ScfContractInfoController extends BladeController {

    private IScfContractInfoService scfContractInfoService;

    private ScfSubjectBasicInfoMapper scfSubjectBasicInfoMapper;

    /**
     * 分页查询合同信息列表
     */
    @GetMapping("/scfGetContractList")
    // @Cacheable(value = "scfContractInfoListCache", key = "'scfContractInfoListCacheKey-' + #scfContractInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetContractListVO>> page(ScfContractInfoDTO scfContractInfo, Query query) {
        if (StringUtils.isEmpty( scfContractInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfContractInfo.setOrgCodes(  scfContractInfo.getOrgCode().split(",") );

        if(scfContractInfo.getOrgCode().contains( "50000000")){
            scfContractInfo.setOrgCodes("50000000".split(",") );
            scfContractInfo.setOrgCode("50000000");
        }

        return R.data(scfContractInfoService.selectScfContractInfoPage(Condition.getPage(query), scfContractInfo));
    }

    /**
     * 查询合同详情信息
     *
     * @param scfContractInfo
     * @return
     */
    @GetMapping("/scfGetContractDetails")
    // @Cacheable(value = "scfContractDetailsCache", key = "'scfContractDetailsCacheKey-' + #scfContractInfo.rowId")
    public R<ScfGetContractDetailsVO> details(ScfContractInfoDTO scfContractInfo) {
        return R.data(scfContractInfoService.getDetails(scfContractInfo));
    }

    /**
     * 新增合同信息
     *
     * @param scfContractInfo
     * @return
     */
    @PostMapping("scfAddContractDetails")
    // @CacheEvict(value = "scfContractInfoListCache", allEntries = true)
    public R<Boolean> save(@RequestBody ScfContractInfoDTO scfContractInfo) throws Exception {
        return R.status(scfContractInfoService.saveContractInfo(scfContractInfo));
    }

    /**
     * 修改合同信息
     *
     * @param scfContractInfo
     * @return
     */
    @PostMapping("scfModContractDetails")
    // @CacheEvict(value = "scfContractDetailsCache", key = "'scfContractDetailsCacheKey-' + #scfContractInfo.rowId")
    public R<Boolean> update(@RequestBody ScfContractInfoDTO scfContractInfo) {
        return R.status(scfContractInfoService.updateContractInfo(scfContractInfo));
    }

    /**
     * 批量导入合同信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfContractBatchFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<T> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfContractInfo> scfContractInfos = scfContractInfoService.batchSaveContractInfo(operatorId, file);
        int bachSaveContractInfo = scfContractInfoService.bachSaveContractInfo(scfContractInfos, operatorId);
        int total = scfContractInfos.size();
        int totalSussess = bachSaveContractInfo;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }

    @PostMapping("/scfContractInfo/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfContractInfoService.deleteByIds( split ));
    }

}
